class publicJs {
  // 时间戳转时间
  fmtDate (obj, pattern) {
    // 判断空
    if (obj === null && obj === null && obj === undefined) {
      console.warn('public 中时间戳不能为空')
      return
    }

    // 判断obj 是不是数字类型
    if (typeof (obj) !== 'number') {
      console.warn('传参类型可能不对')
      return
    }

    // 默认类型
    if (pattern === null | pattern === '' || pattern === undefined) {
      pattern = 'yyyy-mmm-dd hh:mm:ss'
    }

    // 判断obj长度是不是为 16 位
    if (obj.toString().length === 13) {
      obj = obj * 1000
    }

    let date = new Date(obj)
    let y = date.getFullYear()
    let m = (date.getMonth() + 1) < 10 ? ('0' + (date.getMonth() + 1)) : (date.getMonth() + 1)
    let d = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()
    let h = date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours()
    let minute = date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes()
    let second = date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds()
    let day = date.getDay() * 1
    switch (day) {
      case 0:
        return '星期日'
        break
      case 1:
        return '星期一'
        break
      case 2:
        return '星期二'
        break
      case 3:
        return '星期三'
        break
      case 4:
        return '星期四'
        break
      case 5:
        return '星期五'
        break
      case 6:
        return '星期六'
        break
    }

    switch (pattern) {
      case 'yyyy-mmm-dd hh:mm:ss':
        return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
        break
      case 'yyyy-mmm-dd':
        return y + '-' + m + '-' + d
        break
      case 'hh:mm:ss':
        return h + ':' + minute + ':' + second
        break
      case 'chineseYear':
        return h + '年' + minute + '月' + second + '日'
        break
      case 'week':
        return day
        break
    }
  }

  // 表单验证不为空
  formValue (formValue, validate) {
    // formValue 为所传json {userName: boxxer}
    // validate 为 必填写字段 [{key: userName, value: '用户姓名'}]

    if (typeof (formValue) !== 'object') {
      console.warn('formValue 必须是 json')
      return
    }

    if (typeof (validate) !== 'object' && validate.length <= 0) {
      console.warn('validate 必须是 json数组')
      return
    }

    let data = {
      canSubmit: true,
      unableName: null
    }

    validate.map((item) => {
      let keyName = item['key']
      if (keyName in formValue) {
        if (formValue[keyName] === '' || formValue[keyName] === null || formValue[keyName] === undefined) {
          data.canSubmit = false
          data.unableName = item['value']
          return data
        }
      } else {
        data.canSubmit = false
        data.unableName = item['value']
        return data
      }
    })

    // 返回data
    return data
  }

  // 表单元素转data格式
  changeTodata (value) {
    if (value.length > 0 && value instanceof Array) {
      let newData = {}

      value.forEach(item => {
        newData[item['key']] = item.data
      })

      return newData
    } else {
      console.warn('changeTodata传递的参数不是数组')
    }
  }
}

export default publicJs
